package com.ctg.payroll2.service.workflow.imp;

import java.sql.SQLException;

import com.ctg.payroll2.bean.filter.workflow.FlowRoleFilter;
import com.ctg.payroll2.bean.po.SysUser;
import com.ctg.payroll2.bean.po.workflow.FlowRole;
import com.ctg.payroll2.dao.workflow.WorkflowRoleDao;
import com.ctg.payroll2.service.BaseService;
import com.ctg.payroll2.service.workflow.WorkflowRoleService;
import com.ctg.platform.core.security.SecurityUitls;
import com.ctg.platform.core.security.SpringSecurityManager;
import com.ctg.platform.core.security.Exception.RoleNameException;
import com.ctg.platform.core.security.Exception.SecurityException;
import com.ctg.platform.core.security.entity.Role;
import com.ctg.platform.util.Page;

public class WorkflowRoleServiceImp extends BaseService implements
		WorkflowRoleService {
	private WorkflowRoleDao dao;
	/**
	 * RBAC的service
	 */
	private SpringSecurityManager springSecurityManager;

	/**
	 * @param dao
	 *            设置dao
	 */
	public void setDao(WorkflowRoleDao dao) {
		this.dao = dao;
	}

	/**
	 * @param springSecurityManager
	 *            设置springSecurityManager
	 */
	public void setSpringSecurityManager(
			SpringSecurityManager springSecurityManager) {
		this.springSecurityManager = springSecurityManager;
	}

	public int deleteFlowRoleByIds(String ids, SysUser currentUser)
			throws SQLException {
		springSecurityManager.deleteAccountById(ids.split(","), currentUser);
		ids = SecurityUitls.parseIntarraytoStr(ids.split(","), ",");
		return dao.deleteFlowRoleByIds(ids);
	}

	public Long insertFlowRole(FlowRole role, SysUser currentUser)
			throws SecurityException, SQLException, RoleNameException {
		Role secRole = new Role();
		secRole.setName(role.getFlow_rolecode());
		secRole.setDisabled("0");
		String id = springSecurityManager.createSysRole(secRole, currentUser);
		if (id == null || "".equals(id)) {
			return null;
		}
		role.setFlow_roleid(id);
		return dao.insertFlowRole(role);
	}

	public Page queryFlowRoleByFilter(FlowRoleFilter filter) {
		return dao.queryFlowRoleByFilter(filter);
	}

	public Page queryFlowUserByRole(Object paraMap, Integer start, Integer limit) {
		return dao.queryFlowUserByRole(paraMap, start, limit);
	}

	public Page queryRoleUserNotInRoleUser(Object paraMap, Integer start,
			Integer limit) {
		return dao.queryRoleUserNotInRoleUser(paraMap, start, limit);
	}

	/*
	 * paraMap, flow_userid 和 flow_roleid 为必填条件
	 * 
	 * @see com.ctg.payroll2.service.workflow.WorkflowRoleService#deleteRoleUser(java.lang.Object)
	 */
	public Integer deleteRoleUser(Object paraMap) {
		return dao.deleteRoleUser(paraMap);
	}

	public void insertUserByRole(Object paraMap) {
		dao.insertUserByRole(paraMap);
	}
}
